package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.attr.data.CustAttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 服务起始时间-服务截止时间校验 210003980
 * Created by jinchunyu on 2021/4/17 14:47
 */
public class CheckStartTimeAndEndTimeAttrHandler extends AbstractAttrHandler<KiteInst, CustAttrData> {

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, CustAttrData data) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(data.getSceneInstId());
        String askSource = "";
        if (!KiteObjectUtils.isEmpty(sceneInst)) {
            askSource = sceneInst.getAskSource();
        }
        //模版还原时，不做校验,起租等环节不校验,因为好多单子被这个校验住了,页面都进不去了
        String extParam = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "entry");
        if ("sceneTempl".equals(data.getExtAttributes().get("entry")) || "sceneTempl".equals(extParam)
            || KiteStringUtils.isEqual(LocalKeyConsts.ASK_SOURCE_ORDER, askSource)) {
            return;
        }
        //变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))) {
            return;
        }
        // 追加订购
        if (KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.ADD_ORDE))) {
            return;
        }
        int startDate = Integer.parseInt(value.replace("-", ""));
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        int nowdate = Integer.parseInt(df.format(calendar.getTime()));
        if (startDate <= nowdate) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("服务起始时间需要大于当前时间，请重新输入！");
        }
    }

    @Override
    public void after(KiteAttr attr, KiteInst inst, CustAttrData data) {
        //产品变更 延期分组置灰
        if (KiteStringUtils.isEqual(data.getServiceOfferId(), "2826")
            && KiteStringUtils.isEqual(attr.getGroupId(), "300000173")) {
            attr.setIsEdit(KeyConsts.IFTRUE_F);
            operateSceneInstService
                .setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
        }
    }
}
